<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>小兔鲜儿 - 新鲜 惠民 快捷!</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="renderer" content="webkit">
  <link rel="shortcut icon" href="../favicon.ico">
  <link rel="stylesheet" href="./css/common.css">
  <link rel="stylesheet" href="./css/login.css">
  <link rel="stylesheet" href="https://at.alicdn.com/t/font_2143783_iq6z4ey5vu.css">
</head>

<body>
  <!-- 登录头部 -->
  <div class="xtx-login-header">
    <h1 class="logo"></h1>
    <a class="home" href="./index.html">进入网站首页</a>
  </div>
  <!-- 登录内容 -->
  <div class="xtx-login-main">
    <div class="wrapper">
      <form action="">
        <div class="box">
          <div class="tab-nav">
            <a href="javascript:;" class="active">账户登录</a>
            <a href="javascript:;">二维码登录</a>
          </div>
          <div class="tab-pane">
            <div class="link">
              <a href="javascript:;">手机验证码登录</a>
            </div>
            <div class="input">
              <span class="iconfont icon-zhanghao"></span>
              <input required type="text" placeholder="请输入用户名称/手机号码" name="username">
            </div>
            <div class="input">
              <span class="iconfont icon-suo"></span>
              <input required type="password" placeholder="请输入密码" name="password">
            </div>
            <div class="agree">
              <label for="my-checkbox">
                <input type="checkbox" value="1" id="my-checkbox" class="remember" name="agree">
                <span class="iconfont icon-xuanze"></span>
              </label>
              我已同意 <a href="javascript:;">《服务条款》</a href="javascript:;"> 和 <a>《服务条款》</a>
            </div>
            <div class="button clearfix">
              <button type="submit" class="dl">登 录</button>
              <!-- <a class="dl" href="./center.html">登 录</a> -->
              <a class="fl" href="./forget.html">忘记密码？</a>
              <a class="fr" href="./register.html">免费注册</a>
            </div>
          </div>
          <div class="tab-pane" style="display: none;">
            <img class="code" src="../images/code.png" alt="">
          </div>
        </div>
      </form>
    </div>
  </div>
  <!-- 登录底部 -->
  <div class="xtx-login-footer">
    <!-- 版权信息 -->
    <div class="copyright">
      <p>
        <a href="javascript:;">关于我们</a>
        <a href="javascript:;">帮助中心</a>
        <a href="javascript:;">售后服务</a>
        <a href="javascript:;">配送与验收</a>
        <a href="javascript:;">商务合作</a>
        <a href="javascript:;">搜索推荐</a>
        <a href="javascript:;">友情链接</a>
      </p>
      <p>CopyRight &copy; 小兔鲜儿</p>
    </div>
  </div>
  <script>
    // 需求: 记住用户名和密码
    // 1. 获取相关标签
    let nameInput = document.querySelector('input[name=username]')
    let passInput = document.querySelector('input[name=password]')
    let agreeCheckBox = document.querySelector('.remember')
    let theForm = document.querySelector('.wrapper form')
    // 2. form标签 - submit提交事件
    theForm.addEventListener('submit', function(ev){
      // 3. 阻止form自己默认提交行为
      ev.preventDefault()
      // 4. 判断是否勾选协议和输入框是否有值
      if (agreeCheckBox.checked === false || nameInput.value.trim().length === 0 || passInput.value.trim().length === 0) {
        alert('请填写必填项')
        return; // 阻止代码往下走, 输入框为空, 不能下去创建对象存入本地
      }

      // 5. 把账号和密码保存到本地
      let obj = {
        userName: nameInput.value,
        passWord: passInput.value // 以后这里我们需要对密码加密
      }
      let jsonStr = JSON.stringify(obj)
      localStorage.setItem('user', jsonStr)

      // 6. 跳转页面
      location.href = './index.html'
    })
  
  
    // 7. 判断本地是否有值, 如果有证明之前登录过(保存过)
    let jstr = localStorage.getItem('user')
    let userObj = JSON.parse(jstr)
    if (userObj !== null) { // 本地有值
      nameInput.value = userObj.userName
      passInput.value = userObj.passWord
      agreeCheckBox.checked = true
    }
  </script>
</body>

</html>